TOOL.Category = "Construction"
TOOL.Name = "Cannons!"
TOOL.Command = nil
TOOL.ConfigName = "" --Setting this means that you do not have to create external configuration files to define the layout of the tool config-hud 

TOOL.ClientConVar[ "model" ] = "models/cannon.mdl"
TOOL.ClientConVar[ "distmul" ] = 1  
--TOOL.ClientConVar[ "spreadmul" ] = 1
TOOL.ClientConVar[ "fusemin" ] = 2
TOOL.ClientConVar[ "fusemax" ] = 4
--TOOL.ClientConVar[ "loadmin" ] = 3
--TOOL.ClientConVar[ "loadmax" ] = 5
TOOL.ClientConVar[ "shouldexplode" ] = 1
TOOL.ClientConVar[ "shouldignite" ] = 1
TOOL.ClientConVar[ "shoulddestroy" ] = 1
 TOOL.ClientConVar[ "sound" ] = 1

if ( CLIENT ) then
	language.Add( 'Cleanup_cannons', 'Cannons' )
	language.Add( 'Cleaned_cannons', 'Cleaned up all cannons.' )
	language.Add( 'Cleanup_cannonballs', 'Cannon balls' )
	language.Add( 'Cleaned_cannonballs', 'Cleaned up all cannon balls.' )
	
	language.Add( 'Tool_cannon_name', 'Cannons!' )
	language.Add( 'Tool_cannon_desc', 'Spawns custom cannons.' )
	language.Add( 'Tool_cannon_0', 'Left-Click: Spawn Cannon.  Right-Click: Spawn cannon ball.' )
	language.Add( "undone_cannon", "Undone Cannon Piece" )
	language.Add( "sboxlimit_cannons", "You've hit cannons limit!" )
	language.Add( "sboxlimit_cannonballs", "You've hit cannon balls limit!" )
end

if (SERVER) then
	CreateConVar('sbox_cannons_adminonly', 1 )
	CreateConVar('sbox_maxcannons', 3)
	CreateConVar('sbox_maxcannonballs', 5)
end

cleanup.Register( "cannons" )
cleanup.Register( "cannonballs" )

function TOOL:LeftClick( tr )
	if ( GetConVarNumber( "sbox_cannons_adminonly" ) != 0 && !self:GetOwner():IsAdmin() ) then ply:PrintMessage( HUD_PRINTTALK, "You must be an admin to spawn a cannon." )  return end
	if (CLIENT) then return true end
	
	if tr.HitNonWorld then
		if tr.Entity then
			if tr.Entity:IsValid() then
				if tr.Entity:GetClass() == "sd_cannon" then --if we hit an already-existing cannon
					CreateCannon( self:GetOwner(), nil, true, tr.Entity, distmul, spreadmul, fusemin, fusemax, loadmin, loadmax, shouldexplode, shouldignite, shoulddestroy ) --update the current one
					return true
				else
					if (!self:GetSWEP():CheckLimit("cannons")) then return false end
					distmul = self:GetClientNumber("distmul")
					spreadmul = self:GetClientNumber("spreadmul")
					fusemin = self:GetClientNumber("fusemin")
					fusemax = self:GetClientNumber("fusemax")
					loadmin = self:GetClientNumber("loadmin")
					loadmax = self:GetClientNumber("loadmax")
					shouldexplode = self:GetClientNumber("shouldexplode")
					shouldignite = self:GetClientNumber("shouldignite")
					shoulddestroy = self:GetClientNumber("shoulddestroy")
					CreateCannon( self:GetOwner(), tr.HitPos + Vector( 0, 0, 10 ), false, nil, distmul, spreadmul, fusemin, fusemax, loadmin, loadmax, shouldexplode, shouldignite, shoulddestroy ) --make a new one
					return true
				end
			end
		end
	else
		if (!self:GetSWEP():CheckLimit("cannons")) then return false end
		distmul = self:GetClientNumber("distmul")
		spreadmul = self:GetClientNumber("spreadmul")
		fusemin = self:GetClientNumber("fusemin")
		fusemax = self:GetClientNumber("fusemax")
		loadmin = self:GetClientNumber("loadmin")
		loadmax = self:GetClientNumber("loadmax")
		shouldexplode = self:GetClientNumber("shouldexplode")
		shouldignite = self:GetClientNumber("shouldignite")
		shoulddestroy = self:GetClientNumber("shoulddestroy")
		sound = self:GetClientNumber( "sound" )
		CreateCannon( self:GetOwner(), tr.HitPos + Vector( 0, 0, 10 ), false, nil, distmul, spreadmul, fusemin, fusemax, loadmin, loadmax, shouldexplode, shouldignite, shoulddestroy, sound )
		return true
	end
end

function TOOL:RightClick( tr )
	if (CLIENT) then return true end
	--if (!self:GetSWEP():CheckLimit("cannonballs")) then return false end
	canball = ents.Create( "sd_cannonball" )
	canball:SetPos( tr.HitPos + Vector(0,0,10) )
	canball:Spawn()
	undo.Create("cannon") --let us undo it
			undo.AddEntity( canball )
			undo.SetPlayer( self:GetOwner() )
	undo.Finish()
	
	self:GetOwner():AddCleanup( "cannonballs", canball )
	self:GetOwner():AddCount( "cannonballs", canball )
	return true
end

if SERVER then
	function FadeOut( ent )
		if ent:IsValid() then
			ent:SetCollisionGroup( COLLIDE_GROUP_DEBRIS )
			local r,g,b,a = ent:GetColor()
			if a > 150 then
				if ent.sd != nil then
					ent.sd.grow = false
				end
				newA = a-5
				ent:SetColor( r, g, b, newA )
				timer.Simple( 0.1, FadeOut, ent )
			else
				ent:Remove()
			end
		end
	end
	
	function CreateCannon( pl, pos, update, updateent, distmul, spreadmul, fusemin, fusemax, loadmin, loadmax, explode, ignite, destroy, sound )
		if update then
			spawnfx = EffectData()
			spawnfx:SetEntity( updateent )
			util.Effect( "propspawn", spawnfx, true, true )
			updateent.distmul = distmul --give it the new settings
			updateent.spreadmul = spreadmul
			updateent.fusemin = fusemin
			updateent.fusemax = fusemax
			updateent.loadmin = loadmin
			updateent.loadmax = loadmax
			updateent.shouldexplode = shouldexplode
			updateent.shouldignite = shouldignite
			updateent.shoulddestroy = shoulddestroy
			updateent.playsound = sound
		else
			local newcan = ents.Create( "sd_cannon" ) --make a new one and give it settings
			newcan:SetPos( pos )
			newcan.distmul = distmul
			newcan.spreadmul = spreadmul
			newcan.fusemin = fusemin
			newcan.fusemax = fusemax
			newcan.loadmin = loadmin
			newcan.loadmax = loadmax
			newcan.shouldexplode = shouldexplode
			newcan.shouldignite = shouldignite
			newcan.shoulddestroy = shoulddestroy
			newcan.playsound = sound
			newcan:SetColor( 255, 255, 255, 255 )
			newcan:Spawn()
			pl:AddCount( "cannons", newcan )
			local spawnfx = EffectData()
			spawnfx:SetEntity( newcan )
			util.Effect( "propspawn", spawnfx, true, true )
			newcan:Activate()
			
			undo.Create("cannon") --let us undo it
				undo.AddEntity( newcan )
				undo.SetPlayer( pl )
			undo.Finish()
			
			pl:AddCleanup( "cannons", newcan )
		end
	end
end

function TOOL.BuildCPanel( panel )
	panel:AddControl("Header", { Text = "Cannons!", Description = "Happy little cannons." })
	
	panel:AddControl("CheckBox", {
	    Label = "Destroy Constraints",
	    Command = "cannon_shoulddestroy"
    })
	
	panel:AddControl("CheckBox", {
	    Label = "Explode (Kills players)",
	    Command = "cannon_shouldexplode"
    })

	panel:AddControl("CheckBox", {
	    Label = "Ignite Props",
	    Command = "cannon_shouldignite"
    })
	
	panel:AddControl("CheckBox", {
	    Label = "Sound",
	    Command = "cannon_sound"
    })
	
	panel:AddControl("Slider", {
	    Label = "Distance",
	    Type = "Float",
	    Min = "0",
	    Max = "2",
	    Command = "cannon_distmul"
    })
	
	panel:AddControl("Slider", {
	    Label = "Fuse Minimum Time",
	    Type = "Float",
	    Min = "1",
	    Max = "10",
	    Command = "cannon_fusemin"
    })
	
	panel:AddControl("Slider", {
	    Label = "Fuse Maximum Time",
	    Type = "Float",
	    Min = "1",
	    Max = "10",
	    Command = "cannon_fusemax"
    })
end